<?php

namespace App\Admin\Controllers;

use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Utils;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxTag;
use App\Models\Circle\WxCircle;
use App\Models\Shop\WxShopGoods;
use App\Models\WxSlideshow;
use App\Models\User\WxUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxSlideshowController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxSlideshow(), function (Grid $grid) {
            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $grid->model()->where('tenant_id', '=', $__MINI_GLOBAL_TENANT_ID__);
            }
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('poster')->image('',80);
            $grid->column('sort')->editable();
            $grid->column('slideshow_type')->using(FieldUtils::getUrlTypes())->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
            ]);
            $grid->column('target_id', '跳转id');
            $grid->column('slideshow_state')->switch();
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxSlideshow(), function (Show $show) {
            $show->field('id');
            $show->field('poster');
            $show->field('target_id');
            $show->field('slideshow_type');
            $show->field('slideshow_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */

    protected function form()
    {
        return Form::make(new WxSlideshow(), function (Form $form) {
            $form->display('id');
            $form->image('poster')->help('首页轮播建议728*300，SHOP页建议尺寸：750*750，其他：众号首图尺寸：900*383，看着差不多的宽高比例即可')
                ->url('files/uploads')->autoUpload();
            $form->number('sort');
            $form->radio('slideshow_type')->options(FieldUtils::getUrlTypes())->default(0)->required();
            $form->text('target_id', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');

            $form->switch('slideshow_state');


            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $form->text('tenant_id', '本站分应用id')->help('忽略这个配置')->default($__MINI_GLOBAL_TENANT_ID__);
            }

            $form->saving(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                $form->tenant_id = $__MINI_GLOBAL_TENANT_ID__;
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });

    }
}
